package ch.simplix.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;
import ch.simplix.android.flashlight.FlashLightService;
import ch.simplix.android.stroboscope.R;
import ch.simplix.android.stroboscope.R.id;
/**
 * This activity shows how to interact with the ch_simplix_android_flashlight-service
 * @author SimpliX
 *
 */
public class StroboscopeActivity extends Activity {
	private SeekBar darkTimeSeekBar;
	private Button slowerButton;
	private Button fasterButton;
	private ToggleButton flashButton;
	private ToggleButton strobeButton;
	private TextView darkTime;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		darkTime=(TextView)findViewById(R.id.textView4);
		darkTimeSeekBar = (SeekBar) findViewById(R.id.darkTimeSeekBar);
		darkTimeSeekBar.setMax(300);
		darkTimeSeekBar
				.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
					private long latestEvent;

					@Override
					public void onStopTrackingTouch(SeekBar seekBar) {
						darkTime.setText(seekBar.getProgress()+"ms");
						Intent intent = new Intent(
								FlashLightService.FLASH_LIGHT_SERVICE);
						intent.putExtra(FlashLightService.STROBE, true);
						intent.putExtra(FlashLightService.DARK_DELAY,
								seekBar.getProgress());						
						startService(intent);

					}

					@Override
					public void onStartTrackingTouch(SeekBar seekBar) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onProgressChanged(SeekBar seekBar,
							int progress, boolean fromUser) {
						if ((System.currentTimeMillis() - latestEvent) < 250)
							return;
						darkTime.setText(seekBar.getProgress()+"ms");
						Intent intent = new Intent(
								FlashLightService.FLASH_LIGHT_SERVICE);
						intent.putExtra(FlashLightService.STROBE, true);
						intent.putExtra(FlashLightService.DARK_DELAY,
								seekBar.getProgress());
						startService(intent);
						latestEvent = System.currentTimeMillis();
					}
				});

		slowerButton = (Button) findViewById(id.slowerButton);
		slowerButton.setText("\u21e0");
		slowerButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				darkTimeSeekBar.incrementProgressBy(-1);
				Intent intent = new Intent(
						FlashLightService.FLASH_LIGHT_SERVICE);
				intent.putExtra(FlashLightService.STROBE, true);
				intent.putExtra(FlashLightService.DARK_DELAY,
						darkTimeSeekBar.getProgress());
				startService(intent);
			}
		});
		fasterButton = (Button) findViewById(id.fasterButton);
		fasterButton.setText("\u21e2");
		fasterButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				darkTimeSeekBar.incrementProgressBy(1);
				Intent intent = new Intent(
						FlashLightService.FLASH_LIGHT_SERVICE);
				intent.putExtra(FlashLightService.STROBE, true);
				intent.putExtra(FlashLightService.DARK_DELAY,
						darkTimeSeekBar.getProgress());
				startService(intent);
			}
		});

		flashButton = (ToggleButton) findViewById(R.id.toggleFlash);
		strobeButton = (ToggleButton) findViewById(R.id.toggleStrobe);

		flashButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent intent = new Intent(
						FlashLightService.FLASH_LIGHT_SERVICE);
				if (isChecked) {
					strobeButton.setEnabled(false);
					intent.putExtra(FlashLightService.ON, true);

				} else {
					strobeButton.setEnabled(true);
					intent.putExtra(FlashLightService.OFF, true);
				}
				startService(intent);

			}
		});

		strobeButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				Intent intent = new Intent(
						FlashLightService.FLASH_LIGHT_SERVICE);
				intent.putExtra(FlashLightService.STROBE, true);
				
				if (isChecked) {
					flashButton.setEnabled(false);
					intent.putExtra(FlashLightService.ON, true);

				} else {
					flashButton.setEnabled(true);
					intent.putExtra(FlashLightService.OFF, true);
				}
				startService(intent);
			}
		});


	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent(FlashLightService.FLASH_LIGHT_SERVICE);
		intent.putExtra(FlashLightService.STROBE, true);
		intent.putExtra(FlashLightService.OFF, true);
		stopService(intent);
		super.onDestroy();
	}

}
